package com.example.newdemo.net.jsonConverter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Converter;

/**
 *
 */
public class JSONResponseBodyConverters {

    private JSONResponseBodyConverters() {
    }

    static final class JSONObjectResponseBodyConverter implements Converter<ResponseBody, JSONObject> {
        static final JSONObjectResponseBodyConverter INSTANCE = new JSONObjectResponseBodyConverter();


        @Override
        public JSONObject convert(ResponseBody value) throws IOException {
            try {

                JSONObject responseJson=new JSONObject(value.string());
//                LogUtils.systemShow(responseJson.toString());

                return responseJson;
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    static final class JSONArrayResponseBodyConverter implements Converter<ResponseBody, JSONArray> {
        static final JSONArrayResponseBodyConverter INSTANCE = new JSONArrayResponseBodyConverter();

        @Override
        public JSONArray convert(ResponseBody value) throws IOException {
            try {
                JSONArray responseJsonArray= new JSONArray(value.string());
                return responseJsonArray;
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
}